11  R: Objects

11.1 Atomic vectors

An atomic vector in R is a vector containing objects of the same datatype. If the objects are not of the same datatype, then they are coerced to be of the same datatype. It is defined using the keyword c().

numbers = c(1,2,67)

The in-built R function length() is used to find the length of an atomic vector.

length(numbers)
[1] 3

11.1.1 Removing elements from atomic vector

Elements can be removed from the vector using the negative sign within [] brackets.

Remove the 2nd element from the vector:

vec<-1:5
vec[-2]
[1] 1 3 4 5

If multiple elements need to be removed, the indices of the elements to be removed can be given as an atomic vector.

Remove elements 2 to 6 and element 10 from the vector:

vec<-1:20
vec[-c(2:6,10)]
 [1]  1  7  8  9 11 12 13 14 15 16 17 18 19 20

Example: USA’s GDP per capita from 1960 to 2021 is given by the vector G in the code chunk below. The values are arranged in ascending order of the year, i.e., the first value is for 1960, the second value is for 1961, and so on. Store the years in which the GDP per capita of the US increased by more than 10%, in a vector.

G = c(3007, 3067, 3244, 3375,3574, 3828, 4146, 4336, 4696, 5032,5234,5609,6094,6726,7226,7801,8592,9453,10565,11674,12575,13976,14434,15544,17121,18237,19071,20039,21417,22857,23889,24342,25419,26387,27695,28691,29968,31459,32854,34515,36330,37134,37998,39490,41725,44123,46302,48050,48570,47195,48651,50066,51784,53291,55124,56763,57867,59915,62805,65095,63028,69288)
options(digits.secs = 6)
start.time <- Sys.time()

years<-c()
for(i in 1:(length(G)-1))
{
  diff = (G[i+1]-G[i])/G[i]
  if(diff>0.1){years<-c(years,1960+i)}
}
print(years)
[1] 1973 1976 1977 1978 1979 1981 1984
#print(proc.time()[3]-start_time)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
Time difference of 0.009575129 secs

11.2 Matrix

Matrices are two-dimensional arrays. The in-built function matrix() is used to define a matrix. An atomic vector can be organized as a matrix by specifying the number of rows and columns.

For example, let us define a 2x3 matrix (2 rows and 3 columns) consisting of consecutive integers fro1 1 to 6.

mat<-matrix(1:6,2,3)
mat
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Note that the integers fill up column-wise in the matrix. If we wish to fill-up the matrix by row, we can use the byrow argument.

mat<-matrix(1:6,2,3, byrow = TRUE)
mat
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

The functions nrow() and ncol() can be used to get the number of rows and columns of the matrix respectively.

nrow(mat)
[1] 2
ncol(mat)
[1] 3

Matrices can be sliced using the indices of row and column separated by a , in box brackets. Suppose we wish to get the element in the \(2^{nd}\) row and \(3^{rd}\) column of the matrix:

mat[2,3]
[1] 6

For selecting all rows or columns of a matrix, the index for the row/column can be left blank. Suppose we wish to get all the elements of the \(1^{st}\) of the matrix:

mat[1,]
[1] 1 2 3

Row and columns of the matrix can be sliced using the : operator. Suppose we want to select a sub-matrix that has elements in the first two rows and columns 2 and 3 of the matrix mat:

mat[1:2,2:3]
     [,1] [,2]
[1,]    2    3
[2,]    5    6

Suppose we need to sum up all the rows of the matrix. We can do it using a for loop as follows:

mat<-matrix(1:6,2,3)
row_sum<-c(0,0)
for(i in 1:nrow(mat))
{
  for(j in 1:ncol(mat))
  {
    row_sum[i]<-row_sum[i]+mat[i,j]
  }
}
row_sum
[1]  9 12

Observe that in the above for loop, elements of each row are added one at a time. We can add all the elements of a row simultaneously using the sum() function. This will reduce a for loop from the above code:

mat<-matrix(1:6,2,3)
row_sum<-c(0,0)
for(i in 1:nrow(mat))
{
    row_sum[i]<-sum(mat[i,])
}
row_sum
[1]  9 12

In the above code, we sum up all the elements of the row simultaneously. However, we still need to sum up the elements of each row one at a time.

11.2.1 The apply() function

The apply() function is used to apply a function simultaneously on all rows or columns of a matrix

A very useful application of matrices is that parallel computations can be performed on rows or columns of a matrix using the apply() function.